package com.toKnow.controller;

import com.toKnow.model.dto.AuthorInfoDTO;
import com.toKnow.model.dto.UserInfoDTO;
import com.toKnow.model.entity.Article;
import com.toKnow.model.entity.Source;
import com.toKnow.model.vo.*;
import com.toKnow.model.vo.transfer.UserInfoVOTransfer;
import com.toKnow.service.ProfileService;
import com.toKnow.service.ServiceException;
import com.toKnow.service.UserService;
import com.toKnow.utils.JwtUserSessionUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import javax.sql.rowset.serial.SerialException;
import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/api/user")
public class UserController {
    @Autowired
    UserService userService;
    @Autowired
    ProfileService profileService;

    public Integer getUid(HttpSession session){
        try{
            String token = (String) session.getAttribute("userToken");
            return Integer.parseInt(JwtUserSessionUtil.getInfo(token,"id"));
        } catch (Exception e){
            return null;
        }
    }
    @RequestMapping(value = "/collection",method = RequestMethod.POST)
    @ResponseBody
    public BasicVo getCollection(HttpSession session, Integer page){
        try {
            Integer uid = getUid(session);
            if (uid == null) return new BasicVo(false, "没有找到用户ID，可能是没有登录", null);
            List<Article> collection = userService.getCollection(uid, page);
            List<ArticleAbstractVo> ans = new ArrayList<>();
            for (Article article : collection) {
                ans.add(new ArticleAbstractVo(article));
            }
            return new BasicVo(true, null, ans);
        }catch (ServiceException e){
            return new BasicVo(false,e.getMessage(),null);
        }catch (Exception e){
            return new BasicVo(false,"未知错误",null);
        }
    }

    @RequestMapping(value = "/works/article",method = RequestMethod.POST)
    @ResponseBody
    public BasicVo getWorkArticle(HttpSession session, Integer page){
        try {
            Integer uid = getUid(session);
            if (uid == null) return new BasicVo(false, "没有找到用户ID，可能是没有登录", null);
            List<Article> articles = userService.getArticleByAuthor(uid, page);
            List<ArticleAbstractVo> ans = new ArrayList<>();
            for (Article article : articles) {
                ans.add(new ArticleAbstractVo(article));
            }
            return new BasicVo(true, null, ans);
        }catch (ServiceException e){
            return new BasicVo(false,e.getMessage(),null);
        }catch (Exception e){
            return new BasicVo(false,"未知错误",null);
        }
    }
    @RequestMapping(value = "/works/source",method = RequestMethod.POST)
    @ResponseBody
    public BasicVo getWorkSource(HttpSession session, Integer page){
        try {
            Integer uid = getUid(session);
            if (uid == null) return new BasicVo(false, "没有找到用户ID，可能是没有登录", null);
            List<Source> sources = userService.getSourceByAuthor(uid, page);
            List<SourceAbstractVo> ans = new ArrayList<>();
            for (Source source : sources) {
                ans.add(new SourceAbstractVo(source));
            }
            return new BasicVo(true, null, ans);
        }catch (ServiceException e){
            return new BasicVo(false,e.getMessage(),null);
        }catch (Exception e){
            return new BasicVo(false,"未知错误",null);
        }
    }
    @RequestMapping(value = "/browse/history/article",method = RequestMethod.POST)
    @ResponseBody
    public BasicVo getHistoryArticle(HttpSession session, Integer page){
        try {
            Integer uid = getUid(session);
            if (uid == null) return new BasicVo(false, "没有找到用户ID，可能是没有登录", null);
            List<Article> articles = userService.getArticleByBrowseHistory(uid, page);
            List<ArticleAbstractVo> ans = new ArrayList<>();
            for (Article article : articles) {
                ans.add(new ArticleAbstractVo(article));
            }
            return new BasicVo(true, null, ans);
        }catch (ServiceException e){
            return new BasicVo(false,e.getMessage(),null);
        }catch (Exception e){
            return new BasicVo(false,"未知错误",null);
        }
    }
    @RequestMapping(value = "/browse/history/source",method = RequestMethod.POST)
    @ResponseBody
    public BasicVo getHistorySource(HttpSession session, Integer page){
        try {
            Integer uid = getUid(session);
            if (uid == null) return new BasicVo(false, "没有找到用户ID，可能是没有登录", null);
            List<Source> sources = userService.getSourceByBrowseHistory(uid, page);
            List<SourceAbstractVo> ans = new ArrayList<>();
            for (Source source : sources) {
                ans.add(new SourceAbstractVo(source));
            }
            return new BasicVo(true, null, ans);
        }catch (ServiceException e){
            return new BasicVo(false,e.getMessage(),null);
        }catch (Exception e){
            return new BasicVo(false,"未知错误",null);
        }
    }
    @RequestMapping(value = "/search/history")
    @ResponseBody
    public BasicVo getSearchHistory(HttpSession session, Integer page){
        try {
            Integer uid = getUid(session);
            if (uid == null) return new BasicVo(false, "没有找到用户ID，可能是没有登录", null);
            return new BasicVo(true, null, userService.getSearchHistory(uid));
        }catch (ServiceException e){
            return new BasicVo(false,e.getMessage(),null);
        }catch (Exception e){
            return new BasicVo(false,"未知错误",null);
        }
    }

    @RequestMapping(value = "following")
    @ResponseBody
    public BasicVo getFollowing(HttpSession session, Integer page) {
        Integer uid = getUid(session);
        if (uid == null) return new BasicVo(false, "没有找到用户ID，可能是没有登录", null);
        BasicVo cm = new BasicVo();
        List<Integer> uids = userService.getFollowing(uid, page);
        List<AuthorInfoVo> obj = new ArrayList<>();
        for(Integer id : uids) {
            AuthorInfoDTO authorInfoDTO = userService.getAuthorInfo(uid, id);
            AuthorInfoVo vo = new AuthorInfoVo(authorInfoDTO);
            obj.add(vo);
        }
        cm.setObject(obj);
        cm.setSuccess(true);
        cm.setMessage("");
        return cm;
    }
}
